const { createProxyMiddleware: proxy } = require('http-proxy-middleware');

module.exports = function (app) {
    console.log(process.env.ADMIN_API, 'process.env.ADMIN_API')
    app.use(
        proxy('/ezviz', {
            "target": "https://open.ys7.com/",
            changeOrigin: true,
            pathRewrite: { '^/ezviz': '' }
        })
    )
    // app.use(
    //     proxy('/yhgl', {
    //         //"target": "http://172.30.171.203:8889",
    //         "target": "http://218.6.151.129:60080",
    //         changeOrigin: true,
    //         pathRewrite: { '^/yhgl': '' },
    //         onProxyReq: function(proxyReq, req, res) {
    //             // 在代理请求发送到目标服务器之前，添加浏览器保存的 cookie 值到请求头中
    //             proxyReq.setHeader('Cookie', 'JSESSIONID=CgERwMYCkWAyVlC-YCvH6DGAtDF8sySvudObjYpe; username=admin; password=diAr3ghCIlocaS8EzG4hoDboA9K4kyLUUJXTvgLLXVN63kiPazCMsGRfMuFs4OrssavmqoKKrRWv977Q5hgHJg==; rememberMe=true; Authorization=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiJzeXNfdXNlcjoxIiwicm5TdHIiOiJRV0RSRXBGb0dQQUtER29qZzZxNDA3RXRWUkNYRTJHMSJ9.5tu75CDIKYfxRpicqifuNsCEfHMVdsYYwObfhl5Zmrc');
    //             // if (req.headers.cookie) {
    //             //   proxyReq.setHeader('Cookie', req.headers.cookie);
    //             // }
    //           }
    //     })
    // )
    app.use(
        proxy('/admin-api', {
            // "target": "http://shuan.nat300.top",
            // "target": "http://172.16.10.30",
            // "target": "http://218.6.151.129:60080",
            "target": process.env.ADMIN_API || "http://localhost:8888",
            ...(process.env.ADMIN_API ? {} : { pathRewrite: { '^/admin-api': '' } }),
            changeOrigin: true,
            // headers: {
            //     Cookie: 'JSESSIONID=FGXnPPi8zRYuw_MW2FB3_s6FlGbeRLZAMK2d0GiT; rememberMe=true; username=%E6%9D%8E%E6%88%90%E5%AE%87; password=T2O2qGKhXmg1JEotSEiu8HT3y1h+qlNXHi2S9bv945+f/MDxBL18DZr9gbnWboK1+HAip3Gsl5aii1bfrlXmMA==; Authorization=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiJzeXNfdXNlcjoxODQzNjAzMTkwMjEyODAwNTEzIiwicm5TdHIiOiJmU0h6dWU0OE1pRmdVcUdISmZvN21kVml2Y1BFNHpDbSJ9.FSS92kWAbh-zNuZKDbVoxsQvM2hT3kAs2nOMdIjJDU0'
            // }
        })
    )
    app.use(
        proxy('/services/ybgis_namesearch', {
            "target": process.env.SEARCH_API || "http://218.200.175.176:20202",
            changeOrigin: true,
        })
    )
    app.use(
        proxy('/test', {
            "target": "http://localhost:8888",
            changeOrigin: true,
            pathRewrite: { '^/test': '' }
        })
    )
    app.use(
        proxy('/ybyj', {
            "target": "http://172.30.171.203:8889",
            changeOrigin: true
        })
    )
    app.use(
        proxy('/map-services', {
            "target": "https://www.scgis.net/services/",
            changeOrigin: true,
            pathRewrite: { '^/map-services': '' }
        })
    )
    app.use(proxy('/lib', {
        target: 'http://localhost:3000/yj',
        changeOrigin: true,
    }));

    app.use(
        proxy('/terrain', {
            "target": "http://data.mars3d.cn",
            changeOrigin: true
        })
    )
}